package tlcn.app;

import java.util.ArrayList;

import tlcn.business.ConvertUnsigned;
import tlcn.business.ListDishesAdapter;
import tlcn.business.ManagerData;
import tlcn.data.Dish;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;

public class ListDishesActivity extends Activity{
	ArrayList<Dish> dishes;
	ListView listView;
	ListDishesAdapter dishesAdapter;
	ManagerData managerData;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_dishes);
		String date= getIntent().getExtras().getString("date");
		Intent intent = new Intent(getApplicationContext(),GuideActivity.class);
		intent.putExtra("date", date);
		setTitle("Danh sách món ăn");
		managerData= new ManagerData(getApplicationContext());
		dishes=managerData.getDishes();
		listView=(ListView) findViewById(R.id.listViewDish);
		dishesAdapter= new ListDishesAdapter(this, R.layout.item_dish, dishes,intent);
		listView.setAdapter(dishesAdapter);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.search_menu, menu);
		MenuItem item = menu.findItem(R.id.search); 
	    SearchView searchView = (SearchView) item.getActionView();
	    searchView.setOnQueryTextListener(new OnQueryTextListener() {
			@Override
			public boolean onQueryTextSubmit(String query) {
				// TODO Auto-generated method stub
				return false;
			}
			@Override
			public boolean onQueryTextChange(String newText) {
				ConvertUnsigned c= new ConvertUnsigned();
				dishes.clear();
				dishes.addAll(managerData.getDishesByName(c.ConvertString(newText)));
				dishesAdapter.notifyDataSetChanged();				
				return true;
			}
		});
	    return true;
	}

}
